package com.dkd.model.cusServiceCall.controller;


import com.dkd.model.base.constants.PromptMessageEnum;
import com.dkd.model.base.pojo.ReturnData;
import com.dkd.model.base.utils.IdWorker;
import com.dkd.model.base.utils.LoggerUtil;
import com.dkd.model.cusServiceCall.entity.CusServiceCall;
import com.dkd.model.cusServiceCall.entity.CusServiceCallDetail;
import com.dkd.model.cusServiceCall.service.CusServiceCallDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bean
 * @since 2018-09-12
 */
@RestController
@RequestMapping("/cusServiceCallDetail")
public class CusServiceCallDetailController {

    @Autowired
    private CusServiceCallDetailService cusServiceCallDetailService;

    @Value("${IdWorker.workerId}")
    private Integer workerId;

    @Value("${IdWorker.datacenterId}")
    private Integer datacenterId;

    /*
    生成主键ID
     */
    private long getPK(){
        IdWorker idWorker = new IdWorker(workerId, datacenterId);
        return idWorker.nextId();
    }

    // 本地自测获取对象
    private CusServiceCallDetail getOne(){
        CusServiceCallDetail cusServiceCallDetail = new CusServiceCallDetail();
        cusServiceCallDetail.setId(Long.toString(getPK()));
        cusServiceCallDetail.setTitle("TEST"+Math.random());
        //cusServiceCallDetail.setpId("1036905588816216064");
        cusServiceCallDetail.setCreateTime(new Date());
        cusServiceCallDetail.setAllNum(0.01);
        return cusServiceCallDetail;
    }

    private CusServiceCall getHead(int i){
        CusServiceCall cusServiceCall = new CusServiceCall();
        cusServiceCall.setId(getPK()+"");
        cusServiceCall.setBusinessId("一一一"+i);
        cusServiceCall.setCreateTime(new Date());
        cusServiceCall.setErrorCode("二二二"+i);
        cusServiceCall.setPhase("三三三改"+i);
        cusServiceCall.setSendUrl("四四四"+i);
        cusServiceCall.setStatus("0");
        return cusServiceCall;
    }

    /**
     * 插入一条数据
     * http://localhost:8080/cusServiceCallDetail/add
     */
    @RequestMapping(value = "/add")
    public ReturnData<CusServiceCallDetail> add(HttpServletRequest request, CusServiceCallDetail cusServiceCallDetail) {
        LoggerUtil.setDebugLog(getClass(), "CusServiceCallDetailController >> add:测试是否打印log日志~~~~~~~~~~~~~~~~");
        //本地自测得到数据
        cusServiceCallDetail =  getOne();
        //第一种方法
        //boolean result = cusServiceCallDetailService.saveOrUpdate(cusServiceCallDetail);
        //第二种方法
        boolean result = cusServiceCallDetail.insert();
        ReturnData<CusServiceCallDetail> rd = new ReturnData<CusServiceCallDetail>(PromptMessageEnum.SUCCEED,"添加成功",cusServiceCallDetail);
        return rd;
    }

    /**
     * 插入头体数据
     * http://localhost:8080/cusServiceCallDetail/addHeadBody
     */
    @RequestMapping(value = "/addHeadBody")
    public ReturnData<CusServiceCall> add(HttpServletRequest request) {
        CusServiceCall head = getHead(44);
        boolean result = head.insertOrUpdate();
        CusServiceCallDetail cusServiceCallDetail =  getOne();
        cusServiceCallDetail.setpId(head.getId());
        boolean result2 = cusServiceCallDetail.insertOrUpdate();
        List list = Collections.EMPTY_LIST;
        list.add(cusServiceCallDetail);
        head.setDetails(list);
        ReturnData<CusServiceCall> rd = new ReturnData<CusServiceCall>(PromptMessageEnum.SUCCEED,"添加成功",head);
        return rd;
    }

}

